package com.bidevalution.business.job;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.exception.BizException;
import com.qtp.core.util.OConvertUtils;

import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
/**
 * 资信评审（主界面打分）：空值控制
 * @author: 查慧英
 * @date: 2020-02-17 12:37
 */
public class SubmitNullControlJob implements StepJobJdbcFactory {
    private static Logger log = Logger.getLogger(SubmitNullControlJob.class);
    private JdbcTemplateService jdbcTemplateService;
    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId, String layoutCode) throws Exception {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("sectionStepId", beSectionStepJob.getSectionStepId());
            paramMap.put("sectionId", beSectionStepJob.getSectionId());
            paramMap.put("layoutCode", layoutCode);
            paramMap.put("expertId", expertId);
            log.info("layoutCode:"+layoutCode);
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT COUNT(*) as num FROM be_evalution_outcome eo ")
                    .append(" LEFT JOIN be_section_expert_info ei ON eo.expert_id=ei.seq_id ")
                    .append(" WHERE eo.section_id=:sectionId")
                    .append(" AND section_step_id=:sectionStepId")
                    .append(" AND layout_code=:layoutCode")
                    .append(" AND is_manager=1")
                    .append(" AND is_del=0")
                    .append(" AND (c20 IS  NULL OR c20 = '')");
            log.info("判断资信标是否评审完sql:"+sql.toString());
            Map<String, Object> map = jdbcTemplateService.selectForMap(sql.toString(), paramMap);
            if(map != null && !map.isEmpty()) {
                int num = OConvertUtils.getInt(map.get("num"), 0);
                if(num > 0) {
                    throw new BizException("资信标评审还有评审项没有打分。");
                }
            } else {
				throw new BizException("资信标评审还有评审项没有打分。");
			}

        } catch (Exception e) {
            log.error("预审资信标主界面审核错误："+e.getMessage(), e);
            throw e;
        }
        return true;
    }

    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }
}
